<template>
  <div>
    <el-table v-loading="loading" :data="dataSource" @row-click="handleRowClick" @row-contextmenu="rightClick" :height="autoHeight">
      <el-table-column prop="templateName" label="模板名称" align="center">
        <template slot="header" slot-scope="scope">
          <template v-if="select">
            <div>{{ scope.column.label }}</div>
            <el-input  v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
          </template>
          <div v-else>
            <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)" style="width: calc(100% - 50px);padding-right: 8px"></el-input>
            <el-button type="primary" @click="visible = true" icon="el-icon-circle-plus-outline"></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="lab-table-page">
      <div style="display: inline-flex">
        <el-pagination
            :disabled="loading"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 15, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, prev, next"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <el-button-group style="padding: 2px 5px">
          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
        </el-button-group>
      </div>
    </div>

    <el-dialog :visible.sync="visible" title="模板上传" width="400px">
      <el-form label-width="80px" label-position="right">
        <el-form-item label="模板名称">
          <el-input v-model="entityInfo.templateName"></el-input>
        </el-form-item>
        <el-form-item label="模板标签">
          <el-input v-model="entityInfo.templateLabel"></el-input>
        </el-form-item>
        <el-form-item label="数据存储">
          <el-select style="width: 100%" v-model="entityInfo.callDb">
            <template v-for="option in appContext.envs">
              <el-option v-if="option.envType === 'db'" :key="option.envProp" :value="option.envProp" :label="option.envLabel"></el-option>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="文件存储">
          <el-select style="width: 100%" v-model="entityInfo.callStore">
            <template v-for="option in appContext.envs">
              <el-option v-if="option.envType === 'store'" :key="option.envProp" :value="option.envProp" :label="option.envLabel"></el-option>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="资源名称">
          <el-input v-model="entityInfo.nameFormat"></el-input>
        </el-form-item>
        <el-form-item label="实体字段">
          <el-input v-model="entityInfo.entityFormat"></el-input>
        </el-form-item>
        <el-form-item label="分组属性">
          <el-input v-model="entityInfo.groupProp"></el-input>
        </el-form-item>
      </el-form>
      <el-upload
          :action="uploadUrl"
          :headers="headers"
          :data="entityInfo"
          :on-success="uploadSuccess"
          :show-file-list="false"
          drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";

export default {
  name: "AppReportTemplate",
  mixins: [IdeaTable],
  inject: {
    appContext: {
      from: 'appContext',
      default: function () {
        return {}
      }
    }
  },
  props: {
    select: {
      type: Boolean,
      default: false
    }
  },
  computed:{
    autoHeight() {
      return `calc(100vh - 225px)`
    },
    uploadUrl () {
      return this.$Util.mergeUrl(process.env.VUE_APP_BASE_API,  '/reportTemplate/uploadMap')
    },
    headers () {
      return { Authorization: 'Bearer ' + this.$AuthStorage.getAccessToken() }
    }
  },
  created() {
    this.queryUrl = '/reportTemplate/queryPage'
    this.saveUrl = '/reportTemplate/save'
    this.removeUrl = '/reportTemplate/delete/'
    this.rowKey = 'templateId'
    this.rightButtons.splice(0, this.rightButtons.length)
    this.appendRightBtn({ label: '编辑', icon: 'el-icon-edit', click: this.editData })
    this.appendRightBtn({ label: '删除', icon: 'el-icon-delete', click: this.removeData })
    this.appendRightBtn({ label: '下载', icon: 'el-icon-download', click: this.download })
    this.pageInfo.sorts.push({field: 'updateAt', asc: false })
    this.loadSource()
  },
  methods: {
    editData(row) {
      this.entityInfo = this.$Util.copy(row)
      this.visible = true
    },
    uploadSuccess (response) {
      this.$resultMsg(this, response)
      if (response.code === 200) {
        this.loadSource()
        this.visible = false
      }
    },
    download(row) {
      const accessToken = this.$AuthStorage.getAccessToken()
      const url = this.$Util.mergeUrl(process.env.VUE_APP_BASE_API,
          `/reportTemplate/download/${row.templateId}?access_token=${accessToken}`)
      window.open(url)
    }
  }
}
</script>

<style scoped>

</style>
